import Vue from 'vue'
import store from '@/store/'
import VueRouter from 'vue-router'
// import Login from '@/view/Login/'
// import Layout from '@/view/Layout/'
// import Home from '@/view/Home/'
// import User from '@/view/User/'
// import Search from '@/view/Search/'
// import SearchList from '@/view/Search/searchList.vue'
// import ArticleDetail from '@/view/article/'
// import UserEditor from '@/view/User/userinfo.vue'
// import xiaosi from '@/view/User/xiaosi.vue'
Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/layout' // 这里默认打开首页(layout页下的home)
  },
  {
    path: '/login',
    component: () => import(/* webpackChunkName: "login" */ '@/view/Login/')
  },
  {
    path: '/layout',
    component: () => import(/* webpackChunkName: "layout" */ '@/view/Layout'),
    redirect: '/layout/home', // 默认显示二级页面为home首页
    children: [
      {
        path: 'home',
        component: () => import(/* webpackChunkName: "home" */ '@/view/Home/'),
        meta: { // 给当前页面路由对象添加额外信息
          isR: true, // 这个页面有滚动条位置记录(需要修改滚动条) - 标记
          top: 0 // 保存滚动条(卷入)距离
        }
      },
      {
        path: 'user',
        component: () => import(/* webpackChunkName: "user" */ '@/view/User/')
      }
    ]
  },
  { path: '/search', component: () => import(/* webpackChunkName: "Search" */ '@/view/Search/') },
  { path: '/searchList/:keywords', component: () => import(/* webpackChunkName: "searchList" */ '@/view/Search/searchList.vue') },
  {
    path: '/article/:aid', // 文章详情
    component: () => import(/* webpackChunkName: "article" */ '@/view/article/')
  },
  {
    path: '/userinfo', // 用户信息
    component: () => import(/* webpackChunkName: "userinfo" */ '@/view/User/userinfo.vue')
  },
  {
    path: '/xiaosi', // 小思智能
    component: () => import(/* webpackChunkName: "xiaosi" */ '@/view/User/xiaosi.vue')
  }
]
const router = new VueRouter({
  routes
})
router.beforeEach((to, from, next) => {
  // 有token, 不能去登录页
  // 无token, 需要用户"权限"的才需要去登录页
  if (store.state.token.length > 0 && to.path === '/login') {
    // 证明有token-已经登录了
    next(false) // 阻止跳转原地呆着
  } else {
    next()
  }
})
// 全局解析守卫
router.afterEach((to, from) => {
  console.log(to.meta.top)
  // DOM更新(有高度), 再设置滚动位置就ok
  setTimeout(() => {
    window.scrollTo(0, to.meta.top)
  })
})
export default router
